package com.ctshk.app.order.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.req.pay.NotifyConsumeReq;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.goods.dto.OrderDTO;
import com.ctshk.rpc.order.goods.req.OrderAddReq;
import com.ctshk.rpc.order.goods.req.OrderCommodityRefundReq;
import com.ctshk.rpc.order.goods.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * TODO
 *
 * @author 黄利斌
 * @date 2021/5/21 11:03 上午
 */
@Slf4j
@Api(value = "商城C端订单", tags = "商城C端订单")
@RestController
@RequestMapping("/order/commodity/pay")
public class OrderCommodityPaymentController extends BaseController {

    @DubboReference
    private IOrderService orderService;

    @ApiOperation(value = "支付页面订单详情", notes = "支付页面订单详情")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "支付页面订单详情", operDesc = "支付页面订单详情")
    @GetMapping("/info/{id}")
    public Result<OrderDTO> info(@PathVariable Long id) {
        return orderService.info(id);
    }

    @ApiOperation(value = "支付回调接口", notes = "支付回调接口")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "支付回调接口", operDesc = "支付回调接口")
    @PostMapping("/payCallback")
    public Result payCallback(@RequestBody NotifyConsumeReq req) throws Exception {
        TokenUser user = getCurrentUserAllowNotLogIn(getRequest());
        if (null == user) {
            return orderService.payCallback(req, null);
        } else {
            return orderService.payCallback(req, getCurrentUser());
        }
    }

    @ApiOperation(value = "商品訂單支付-唤起支付", notes = "商品訂單支付-唤起支付")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "商品訂單支付", operDesc = "商品訂單支付")
    @PostMapping("/payment")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "JSON格式传递：例如{'orderId':'123123123123' //订单id,'paymentMethodCode':'1-5' // 支付方式,'shopId':'123123123' // 小店id}", name = "req")
    })
    public Result payment(@RequestBody JSONObject req) throws Exception {
        Long orderId = req.getLong("orderId");
        Integer paymentMethodCode = req.getInteger("paymentMethodCode");
        Long shopId = req.getLong("shopId");
        return orderService.payPlatform(orderId, paymentMethodCode, shopId);
    }

    @ApiOperation(value = "取消訂單", notes = "取消訂單")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "取消訂單", operDesc = "取消訂單")
    @PostMapping("/cancelAppOrder")
    public Result cancelAppOrder(@RequestBody @Valid List<OrderCommodityRefundReq> refundReqs) throws Exception {
        orderService.cancelAppOrder(refundReqs, getCurrentUser(getRequest()));
        return Result.success();
    }

    @ApiOperation(value = "新增門店訂單", notes = "新增門店訂單")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "新增門店訂單", operDesc = "新增門店訂單")
    @PostMapping("/addAppOrder")
    public Result<Long> addAppOrder(@RequestBody @Valid OrderAddReq obj) throws Exception {
        if (null == obj.getUserId()) {
            return orderService.addAppOrder(obj, null);
        } else {
            return orderService.addAppOrder(obj, obj.getUserId());
        }
//        return Result.failed(SystemError.ma);
    }
}
